// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
require('es6-promise').polyfill()
import Vue from 'vue'
import App from './App'
import router from './router'
import axios from 'axios'
import 'babel-polyfill'
// import 'promise-polyfill/src/polyfill'
import '@/plugins/flexible'
import MintUI from 'mint-ui'
import store from '@/store'
//import FastClick from "fastclick";
import '@/utils/directives'
import './filters'
import VueScroller from 'vue-scroller'
//import VeeValidate from "vee-validate";
import '@/plugins/validator/validator.js' //vue验证
// import VueInput from "vue-input"; //解决移动端input框被输入法面新版遮挡的问题
// Vue.use(VueInput);
//import VCconsole from "vconsole";
//icon
require('@/assets/common/fonts/iconfont.css')

// if ("addEventListener" in document) {
//   document.addEventListener(
//     "DOMContentLoaded",
//     function() {
//       FastClick.attach(document.body);
//     },
//     false
//   );
// }

Vue.use(MintUI)
Vue.use(VueScroller)

import { getScrollTop, setScrollTop } from '@/utils/mixin'
let routerList = []
router.beforeEach((to, from, next) => {
  //返回上一级浏览位置
  let position = getScrollTop()
  let currentRouterIndex = routerList.findIndex(e => {
    return e.path === from.fullPath
  })

  if (currentRouterIndex != -1) {
    routerList[currentRouterIndex].position = position
  } else {
    routerList.push({
      path: from.fullPath,
      position: position
    })
  }
  //路由控制
  let token = window.sessionStorage.getItem('token')

  if (token) {
    if (to.path == '/login') {
      next('/')
      return false
    }
    store.dispatch('GetUserInfo', { user_token: token }).then(
      response => {
        next()
      },
      err => {
        window.sessionStorage.removeItem('token')
      }
    )
  } else {
    if (to.query.sign == undefined && to.path != '/login') {
      next({
        path: '/login',
        query: { redirect: to.fullPath } // 将跳转的路由path作为参数，登录成功后跳转到该路由
      })
    } else {
      next()
    }
  }
})

router.afterEach((to, from, next) => {
  //解决 keekAlive导致列表页不刷新
  let savedPosition = routerList.find(e => {
    return e.path === to.fullPath
  })

  if (typeof savedPosition !== 'undefined') {
    Vue.nextTick(() => {
      setScrollTop(savedPosition.position)
    })
  } else {
    Vue.nextTick(() => {
      setScrollTop(0)
    })
  }
})

Vue.config.productionTip = false

/* fundebug */
// function formatComponentName(vm) {
//   if (vm.$root === vm) return 'root'

//   var name = vm._isVue
//     ? (vm.$options && vm.$options.name) ||
//       (vm.$options && vm.$options._componentTag)
//     : vm.name
//   return (
//     (name ? 'component <' + name + '>' : 'anonymous component') +
//     (vm._isVue && vm.$options && vm.$options.__file
//       ? ' at ' + (vm.$options && vm.$options.__file)
//       : '')
//   )
// }

// Vue.config.errorHandler = function(err, vm, info) {
//   var componentName = formatComponentName(vm)
//   var propsData = vm.$options && vm.$options.propsData

//   fundebug.notifyError(err, {
//     metaData: {
//       componentName: componentName,
//       propsData: propsData,
//       info: info
//     }
//   })
// }

/* eslint-disable no-new */
Vue.prototype.errorEvent = e => {
  e.target.src = './assets/images/logo.png'
}
new Vue({
  el: '#app',
  store,
  router,
  template: '<App/>',
  components: { App }
})
